/**
 * 函数对象
 */
var Validate = function () {

    /**
     * 初始化 jquery validation
     */
    var handlerInitValidate = function () {
        $("#inputForm").validate({
            rules: {
                email: {
                    required: true,
                },
                password: {
                    required: true,
                },
                username: {
                    required: true,
                },
                phone: {
                    required: true,
                    phone: true,
                }
            },
            messages: {
                email: {
                    required: "请输入电子邮箱",
                },
                password: {
                    required: "请输入密码",
                },
                username: {
                    required: "请输入用户名",
                },
                phone: {
                    required: "请输入手机号",
                    phone: "手机号格式有误"
                }
            },
            errorElement: 'span',
            errorPlacement: function (error, element) {
                error.addClass('invalid-feedback');
                element.closest('.form-group').append(error);
            },
            highlight: function (element, errorClass, validClass) {
                $(element).addClass('is-invalid');
            },
            unhighlight: function (element, errorClass, validClass) {
                $(element).removeClass('is-invalid');
            }
        });
    }

    /**
     * 增加自定义验证规则
     */
    var handlerInitCustomValidate = function () {
        $.validator.addMethod("phone", function(value, element) {
            var length = value.length;
            var mobile =  /^1[3456789]\d{9}$/;
            return this.optional(element) || (length == 11 && mobile.test(value));
        }, "手机号码格式错误");
    };

    return {
        /**
         * 初始化
         */
        init: function () {
            handlerInitCustomValidate();
            handlerInitValidate();
        }
    }

}();

$(document).ready(function () {
    Validate.init();
});